﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BaseDatos;

namespace SCHD
{
    public partial class WFModulos : TDV.Controles.Base.WinForm.BaseForm_Tipo02
    {
        public WFModulos()
        {
            InitializeComponent();
        }

        private void TsbNuevo_Click(object sender, EventArgs e)
        {
            string codform = "";
            int contador = 0;
            foreach (DataGridViewRow Row in GrdFormularios.Rows)
            {
                contador = contador + 1;
                if (contador > 1)
                {
                    codform = codform + ",'" + Row.Cells[1].Value.ToString() + "'";
                }
                else
                {
                    codform = codform + "'" + Row.Cells[1].Value.ToString() + "'";
                }
            }
            WFModulo_Edit formEdit = new WFModulo_Edit(EModoformulario.Insertar, "", "", "", "", "", "", "", "", "", codform);
            formEdit.ShowDialog();
            if (formEdit.DialogResult == DialogResult.OK)
            {
                Cargar();
            }
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void WFFormularios_Load(object sender, EventArgs e)
        {
            Cargar();
            TxtCodigo.CharacterCasing = CharacterCasing.Upper;
            TxtNombre.CharacterCasing = CharacterCasing.Upper;
            TxtDescripcion.CharacterCasing = CharacterCasing.Upper;
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            Cargar();
        }
        protected void Cargar()
        {
            GrdFormularios.DataSource = null;
            string SQL = "" +
                      " select (case when cmo.flagpadpopup = 'D' then 'Principal' else 'Secundario' end) Tipo, cmo.FLAGLINEA,cmo.orden,cmo.FLAGPADPOPUP, cmo.codigosistema,cmo.codigomodulo, codform,nomform,dscform,comando from chdformularios left join CHDMODULOS_OPCIONES cmo " +
                      " on codform = cmo.CODIGOOPCION " +
                      " where codform = nvl('"+TxtCodigo.Text+"',codform) " +
                      " and nomform like '%"+TxtNombre.Text+"%' "+
                      " and dscform like '%"+TxtDescripcion.Text+"%' "+
                      " and cmo.flageliminado = '0'"+
                      " and flgeli ='0' order by cmo.codigoopcion,cmo.orden";
            DataSet ds = ConexionBaseDatos.Conexion.CrearDataSet(SQL,CommandType.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GrdFormularios.DataSource = ds.Tables[0];
            }
        }

        private void TsbGuardar_Click(object sender, EventArgs e)
        {
           
        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            //Eliminar el Módulo
            string codigoSistema = GrdFormularios.CurrentRow.Cells["cSistema"].Value.ToString();
            string codigoModulo = GrdFormularios.CurrentRow.Cells["cModulo"].Value.ToString();
            string opcion = GrdFormularios.CurrentRow.Cells["cCodigo"].Value.ToString();
            string SQL = " delete chdmodulos_opciones where  " +
                        " codigosistema = '" + codigoSistema + "' and codigomodulo = '" + codigoModulo + "' and codigoopcion = '" + opcion + "' and flageliminado ='0'";
            ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
            Cargar();
        }

        private void TsbModificar_Click(object sender, EventArgs e)
        {
            try
            {
            string orden = GrdFormularios.CurrentRow.Cells["cOrden"].Value.ToString();
            string codigo = GrdFormularios.CurrentRow.Cells["cCodigo"].Value.ToString();
            string sistema = GrdFormularios.CurrentRow.Cells["cSistema"].Value.ToString();
            string modulo = GrdFormularios.CurrentRow.Cells["cModulo"].Value.ToString();
            string nombre =  GrdFormularios.CurrentRow.Cells["Nombre"].Value.ToString();
            string descripcion = GrdFormularios.CurrentRow.Cells["Descripcion"].Value.ToString();
            string comando  = GrdFormularios.CurrentRow.Cells["Comando"].Value.ToString();
            string flagpopup = GrdFormularios.CurrentRow.Cells["FLAGPOPUP"].Value.ToString();
            string linea =  GrdFormularios.CurrentRow.Cells["cLinea"].Value.ToString();
            WFModulo_Edit formedit = new WFModulo_Edit(EModoformulario.Modificar, orden, codigo, sistema, modulo, nombre, descripcion, comando, flagpopup, linea,"");
            formedit.ShowDialog();
            if(formedit.DialogResult == DialogResult.OK)
            {
                Cargar();
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TsbListaFormularios_Click(object sender, EventArgs e)
        {
            string codigo = "";
            int contador = 0;
            foreach (DataGridViewRow Row in GrdFormularios.Rows)
            {
                contador = contador + 1;
                if (contador > 1)
                {
                    codigo = codigo + ",'" + Row.Cells[1].Value.ToString() + "'";
                }
                else
                {
                    codigo = codigo + "'" + Row.Cells[1].Value.ToString() + "'";
                }
            }

            WFListFormularios form = new WFListFormularios(codigo);
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                //Insertar;
                // form.CodigoFormulario;
            }
        }

        private void TsbFormularios_Click(object sender, EventArgs e)
        {
            WFFormularios formularios = new WFFormularios();
            formularios.Show();
        }
    }
}
